﻿#include <Python.h>
#include "kbe.h"
#include "log.h"
#include "config.h"
#include "resmgr.h"
#include "common/common.h"
#include "pyscript/script.h"
#include <string>
#include <iostream>

using namespace xpy;
using namespace KBEngine;

namespace KBEngine {
    COMPONENT_TYPE g_componentType = CLIENT_TYPE;
    COMPONENT_ORDER g_componentGlobalOrder = 1;
    COMPONENT_ORDER g_componentGroupOrder = 1;
    COMPONENT_ID g_componentID = 1;
    int32 g_genuuid_sections = -1;
}

KBEngine::script::Script* g_pScript = nullptr;

bool kbe_init()
{
    ResMgr::instance();   // 需要执行这一句，创建一个实例。如果没有创建实例，访问没有初始化的成员变量会出问题

    g_componentID = genUUID64();
    g_componentType = CLIENT_TYPE;

    Config *pConfig = new Config();
    std::string jsonStr = ResMgr::getInstance()->readFileText(ResMgr::getInstance()->getResPath() + "/kbengine.xml");
    if (!pConfig->loadJsonStr(jsonStr.c_str()))
    {
        logger::error("loadJsonStr() is error!\n");
        return false;
    }

    delete pConfig;

//
//    DebugHelper::initialize(g_componentType);
//
    logger::info("-----------------------------------------------------------------------------------------\n\n\n");
//
//#ifdef USE_OPENSSL
//    if (KBEngine::KBEKey::getSingletonPtr() == NULL)
//        KBEngine::KBEKey kbekey(KBEngine::Resmgr::getSingleton().matchPath("key/") + "kbengine_public.key", "");
//#endif
//
    if (g_pScript == nullptr)
        g_pScript = new KBEngine::script::Script();
//
//    if (g_pDispatcher == NULL)
//        g_pDispatcher = new Network::EventDispatcher();
//
//    if (g_pNetworkInterface == NULL)
//    {
//        g_pNetworkInterface = new Network::NetworkInterface(g_pDispatcher,
//                                                            0, 0, "", 0, 0,
//                                                            0, "", 0, 0);
//    }
//
//    if (!installPyScript(*g_pScript, g_componentType))
//    {
//        ERROR_MSG("installPyScript() is error!\n");
//        return false;
//    }
//
//    g_pApp = new ClientApp(*g_pDispatcher, *g_pNetworkInterface, g_componentType, g_componentID);
//    g_pApp->setScript(g_pScript);
//
//    START_MSG(COMPONENT_NAME_EX(g_componentType), g_componentID);
//    if (!g_pApp->initialize())
//    {
//        ERROR_MSG("app::initialize is error!\n");
//        g_pApp->finalise();
//        Py_DECREF(g_pApp);
//        g_pApp = NULL;
//
//        uninstallPyScript(*g_pScript);
//
//        SAFE_RELEASE(g_pNetworkInterface);
//        SAFE_RELEASE(g_pScript);
//        SAFE_RELEASE(g_pDispatcher);
//        SAFE_RELEASE(pserverconfig);
//        SAFE_RELEASE(pconfig);
//        return false;
//    }
//
//    if (g_pTelnetServer == NULL)
//    {
//        g_pTelnetServer = new TelnetServer(g_pDispatcher, g_pNetworkInterface);
//        g_pTelnetServer->pScript(g_pScript);
//        if (!g_pTelnetServer->start(pconfig->telnet_passwd,
//                                    pconfig->telnet_deflayer,
//                                    pconfig->telnet_port))
//        {
//            ERROR_MSG("app::initialize: TelnetServer is error!\n");
//            return false;
//        }
//    }
//    else
//    {
//        g_pTelnetServer->pScript(g_pScript);
//    }
//
//    if (g_pThreadPool == NULL)
//    {
//        g_pThreadPool = new thread::ThreadPool();
//        if (!g_pThreadPool->isInitialize())
//        {
//            g_pThreadPool->createThreadPool(1, 1, 4);
//        }
//        else
//        {
//            ERROR_MSG("g_threadPool.isInitialize() is error!\n");
//            return false;
//        }
//    }
//
//    INFO_MSG(fmt::format("---- {} is running ----\n", COMPONENT_NAME_EX(g_componentType)));
//
//    PyEval_ReleaseThread(PyThreadState_Get());
//    KBEConcurrency::setMainThreadIdleCallbacks(&releaseLock, &acquireLock);
//
//    g_pThreadPool->addTask(new KBEMainTask());
    return true;
}
